added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / CSEnumerateAppDomains / Program.cs
blobf86e9334e85619630dc8a28ccedb799e72acec29
1 /****************************** Module Header ******************************\
2 * Module Name: Program.cs
3 * Project: CSEnumerateAppDomains
4 * Copyright (c) Microsoft Corporation.
5 *
6 * This source file is used to handle the input command. If this application
7 * starts with an argument, process the command directly and then exit, else
8 * show the help text to let user choose a command.
9 *
10 * This source is subject to the Microsoft Public License.
11 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
12 * All other rights reserved.
14 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
15 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
17 \***************************************************************************/
19 using System;
20 using Microsoft.Samples.Debugging.CorDebug;
22 namespace CSEnumerateAppDomains
24 class Program
26 static void Main(string[] args)
29 // Create new AppDomain in current process.
30 AppDomain.CreateDomain("Hello world!");
32 try
34 // If this application starts without any argument, show the help text to
35 // let user choose a command.
36 if (args.Length == 0)
38 // Application will not exit until user types the exit command.
39 // If the command is not correct, it will show the help text in the
40 // next loop.
41 while (true)
43 Console.WriteLine(@"
44 Please choose a command:
45 1: Show AppDomains in current process.
46 2: List all managed processes.
47 3: Show help text.
48 4: Exit this application.
49 To show the AppDomains in a specified process, please type ""PID"" and
50 the ID of the process directly, like PID1234.
51 ");
53 string cmd = Console.ReadLine();
54 int cmdID = 0;
55 if (int.TryParse(cmd, out cmdID))
57 switch (cmdID)
59 case 1:
60 ProcessCommand("CurrentProcess");
61 break;
62 case 2:
63 ProcessCommand("ListAllManagedProcesses");
64 break;
65 case 4:
66 Environment.Exit(0);
67 break;
68 default:
70 // Show the help text in the next loop.
71 break;
75 else if (cmd.StartsWith("PID", StringComparison.OrdinalIgnoreCase))
77 ProcessCommand(cmd);
82 else if (args.Length == 1)
84 ProcessCommand(args[0]);
87 catch (Exception ex)
89 Console.WriteLine(ex.Message);
91 // The exit code 100 means that this application does not run successfully.
92 Environment.Exit(100);
96 static void ProcessCommand(string arg)
98 // List AppDomains in current process.
99 if (arg.Equals("CurrentProcess", StringComparison.OrdinalIgnoreCase))
101 Console.WriteLine("List AppDomains in current process...");
102 ShowAppDomainsInCurrentProcess();
105 // List all managed processes.
106 else if (arg.Equals("ListAllManagedProcesses", StringComparison.OrdinalIgnoreCase))
108 Console.WriteLine("List all managed processes...");
109 ListAllManagedProcesses();
112 // Show the AppDomains in a specified process, arg must starts with "PID".
113 else if (arg.StartsWith("PID", StringComparison.OrdinalIgnoreCase))
115 int pid = 0;
116 int.TryParse(arg.Substring(3), out pid);
117 Console.WriteLine(string.Format(
118 "List AppDomains in the process {0} ...", pid));
119 ShowAppDomains(pid);
122 else
124 throw new ArgumentException("Please type a valid command.");
129 /// <summary>
130 /// Show AppDomains in Current Process.
131 /// </summary>
132 static void ShowAppDomainsInCurrentProcess()
135 // GetAppDomainsInCurrentProcess is a static method of the class ManagedProcess.
136 // This method is used to get all AppDomains in Current Process.
137 var appDomains = ManagedProcess.GetAppDomainsInCurrentProcess();
139 foreach (var appDomain in appDomains)
141 Console.WriteLine("AppDomain Id={0}, Name={1}",
142 appDomain.Id, appDomain.FriendlyName);
146 /// <summary>
147 /// Show AppDomains in a specified process.
148 /// </summary>
149 /// <param name="pid"> The ID of the Process.</param>
150 static void ShowAppDomains(int pid)
152 if (pid <= 0)
154 throw new ArgumentException("Please type a valid PID.");
157 ManagedProcess process = null;
160 // GetManagedProcessByID is a static method of the class ManagedProcess.
161 // This method is used to get an instance of ManagedProcessInfo. If there is
162 // no managed process with this PID, an ArgumentException will be thrown.
163 process = ManagedProcess.GetManagedProcessByID(pid);
165 foreach (CorAppDomain appDomain in process.AppDomains)
167 Console.WriteLine("AppDomain Id={0}, Name={1}",
168 appDomain.Id,
169 appDomain.Name);
173 catch (ArgumentException _argumentException)
175 Console.WriteLine(_argumentException.Message);
177 catch (ApplicationException _applicationException)
179 Console.WriteLine(_applicationException.Message);
181 catch (Exception ex)
183 Console.WriteLine(ex.Message);
184 Console.WriteLine(ex.GetType());
185 Console.WriteLine(ex.StackTrace);
186 Console.WriteLine("Cannot get the process. "
187 + " Make sure the process exists and it's a managed process");
189 finally
191 if (process != null)
193 process.Dispose();
198 /// <summary>
199 /// List all managed processes.
200 /// </summary>
201 static void ListAllManagedProcesses()
204 // GetManagedProcesses is a static method of the class ManagedProcess.
205 // This method is used to get a list that contains all managed processes
206 // in current machine.
207 var processes = ManagedProcess.GetManagedProcesses();
209 foreach (var process in processes)
211 Console.WriteLine("ID={0}\tName={1}",
212 process.ProcessID, process.ProcessName);
213 Console.Write("Loaded Runtimes: ");
214 foreach (var runtime in process.LoadedRuntimes)
216 Console.Write(runtime.GetVersionString() + "\t");
218 Console.WriteLine("\n");